home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / collect / colledoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.9 KB  |  318 lines

  1. // colledoc.cpp : implementation of the CCollectDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"                              
  14. #include "collect.h"
  15.                                      
  16. #include "colledoc.h"
  17. #include "resource.h"                           
  18.   
  19. #ifdef _DEBUG                                          
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CMyStruct
  26.  
  27. void CMyStruct::FormatMyStruct(CString& str)
  28. {
  29.     if(m_char == _T('\0'))
  30.         str.Format(_T("{%d, , %ls}"), m_int, m_str);
  31.     else
  32.         str.Format(_T("{%d, %lc, %ls}"), m_int, m_char, m_str);
  33. }
  34.  
  35. void AFXAPI SerializeElements(CArchive& ar, CMyStruct** ppElements, int nCount)
  36. {
  37.     // Since SerializeElements is always called by the framework with nCount=1
  38.     // for a CMap<>, it is a good idea to implement SerializeElement to handle
  39.     // nCount>1, in case you decide to reuse it for a CArray<> with the same
  40.     // element type.
  41.     if (ar.IsStoring())
  42.     {
  43.         for (int i = 0; i < nCount; i++)
  44.         {
  45.             CMyStruct* pMyStruct = *(ppElements + i);
  46.             WORD w = (WORD)pMyStruct->m_int;
  47.             ar << w;
  48.             ar << pMyStruct->m_char;
  49.             ar << pMyStruct->m_str;
  50.             nCount--;
  51.         }
  52.     }
  53.     else
  54.     {
  55.         for (int i = 0; i < nCount; i++)
  56.         {
  57.             CMyStruct* pMyStruct = new CMyStruct;
  58.             *(ppElements + i) = pMyStruct;
  59.             WORD w;
  60.             ar >> w;
  61.             pMyStruct->m_int = w;
  62.             ar >> pMyStruct->m_char;
  63.             ar >> pMyStruct->m_str;
  64.         }
  65.     }
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMyObject
  70.  
  71. IMPLEMENT_SERIAL(CMyObject, CObject, 0)
  72.  
  73. CMyObject::CMyObject()
  74. {
  75. }
  76.  
  77. CMyObject::~CMyObject()
  78. {
  79. }
  80.  
  81. void CMyObject::FormatMyObject(CString& str)
  82. {
  83.     if(m_char == _T('\0'))
  84.         str.Format(_T("{%i, , %ls}"), m_int, m_str);
  85.     else
  86.         str.Format(_T("{%i, %lc, %ls}"), m_int, m_char, m_str);
  87. }
  88.  
  89. void CMyObject::Serialize(CArchive& ar)
  90. {
  91.     WORD w;
  92.     if (ar.IsStoring())
  93.     {
  94.         w = (WORD)m_int;
  95.         ar << w;
  96.         ar << m_char;
  97.         ar << m_str;
  98.     }
  99.     else
  100.     {
  101.         ar >> w;
  102.         m_int = w;
  103.         ar >> m_char;
  104.         ar >> m_str;
  105.     }
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CCollectDoc
  110.  
  111. IMPLEMENT_DYNCREATE(CCollectDoc, CDocument)
  112.  
  113. BEGIN_MESSAGE_MAP(CCollectDoc, CDocument)
  114.     //{{AFX_MSG_MAP(CCollectDoc)
  115.     //}}AFX_MSG_MAP
  116. END_MESSAGE_MAP()
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CCollectDoc construction/destruction
  120.  
  121. CCollectDoc::CCollectDoc()
  122. {
  123. }
  124.  
  125. CCollectDoc::~CCollectDoc()
  126. {
  127. }
  128.  
  129. BOOL CCollectDoc::OnNewDocument()
  130. {
  131.     if (!CDocument::OnNewDocument())
  132.         return FALSE;
  133.  
  134.     CString strFirst;
  135.     strFirst.LoadString(IDS_INITIAL_STRING);
  136.     m_stringList.AddTail(strFirst);
  137.  
  138.     CMyStruct* pMyStruct = new CMyStruct();
  139.     pMyStruct->m_int = 1234;
  140.     pMyStruct->m_str.LoadString(IDS_INITIAL_STRING);
  141.     pMyStruct->m_char = pMyStruct->m_str[0];
  142.     m_mystructList.AddTail(pMyStruct);
  143.  
  144.     m_intList.AddTail(100);
  145.  
  146.     m_dwArray.Add(100000);
  147.  
  148.     CMyObject* pMyObject = new CMyObject();
  149.     pMyObject->m_int = 5678;
  150.     pMyObject->m_str.LoadString(IDS_INITIAL_STRING);
  151.     pMyObject->m_char = pMyObject->m_str[0];
  152.     m_myobArray.Add(pMyObject);
  153.  
  154.     CPoint pt(10,10);
  155.     m_ptArray.Add(pt);
  156.  
  157.     CString strKey, strValue;
  158.     strKey.LoadString(IDS_INITIAL_KEY);
  159.     strValue.LoadString(IDS_INITIAL_VALUE);
  160.     m_mapStringToString[strKey] = strValue;
  161.  
  162.  
  163.     CMyObject* pMyObject2 = new CMyObject();
  164.     pMyObject2->m_int = 1357;
  165.     pMyObject2->m_str.LoadString(IDS_INITIAL_STRING);
  166.     pMyObject2->m_char = pMyObject2->m_str[0];
  167.     m_mapStringToMyObject[strKey] = pMyObject2;
  168.  
  169.     CMyStruct* pMyStruct2 = new CMyStruct();
  170.     pMyStruct2->m_int = 2468;
  171.     pMyStruct2->m_str.LoadString(IDS_INITIAL_STRING);
  172.     pMyStruct2->m_char = pMyStruct2->m_str[0];
  173.     m_mapDWordToMyStruct[100] = pMyStruct2;
  174.  
  175.     return TRUE;
  176. }
  177.  
  178. void CCollectDoc::DeleteContents()
  179. {
  180.     m_stringList.RemoveAll();
  181.  
  182.     POSITION pos = m_mystructList.GetHeadPosition();
  183.     while (pos != NULL)
  184.     {
  185.         delete m_mystructList.GetNext(pos);
  186.     }
  187.     m_mystructList.RemoveAll();
  188.  
  189.     m_intList.RemoveAll();
  190.  
  191.     m_dwArray.RemoveAll();
  192.  
  193.     for (int n = 0; n < m_myobArray.GetSize(); n++)
  194.     {
  195.         delete m_myobArray[n];
  196.     }
  197.     m_myobArray.RemoveAll();
  198.  
  199.     m_mapStringToString.RemoveAll();
  200.  
  201.     m_ptArray.RemoveAll();
  202.  
  203.     pos = m_mapStringToMyObject.GetStartPosition();
  204.     while (pos != NULL)
  205.     {
  206.         CString str;
  207.         CMyObject* pMyObject;
  208.         m_mapStringToMyObject.GetNextAssoc(pos, str, pMyObject);
  209.         delete pMyObject;
  210.     }
  211.     m_mapStringToMyObject.RemoveAll();
  212.  
  213.     pos = m_mapDWordToMyStruct.GetStartPosition();
  214.     while (pos != NULL)
  215.     {
  216.         DWORD dwKey;
  217.         CMyStruct* pMyStruct;
  218.         m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct);
  219.         delete pMyStruct;
  220.     }
  221.     m_mapDWordToMyStruct.RemoveAll();
  222. }
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225. // CCollectDoc serialization
  226.  
  227. void CCollectDoc::Serialize(CArchive& ar)
  228. {
  229.     POSITION pos;
  230.     WORD nCount;
  231.     WORD w;
  232.  
  233.     m_stringList.Serialize(ar);
  234.  
  235.     if (ar.IsStoring())
  236.     {
  237.         nCount = (WORD)m_mystructList.GetCount();
  238.         ar << nCount;
  239.         pos = m_mystructList.GetHeadPosition();
  240.         while (pos != NULL)
  241.         {
  242.             CMyStruct* pMyStruct = m_mystructList.GetNext(pos);
  243.             w = (WORD)pMyStruct->m_int;
  244.             ar << w;
  245.             ar << pMyStruct->m_char;
  246.             ar << pMyStruct->m_str;
  247.             nCount--;
  248.         }
  249.         ASSERT(nCount == 0);
  250.     }
  251.     else
  252.     {
  253.         ar >> nCount;
  254.         while (nCount-- > 0)
  255.         {
  256.             CMyStruct* pMyStruct = new CMyStruct;
  257.             ar >> w;
  258.             pMyStruct->m_int = w;
  259.             ar >> pMyStruct->m_char;
  260.             ar >> pMyStruct->m_str;
  261.             m_mystructList.AddTail(pMyStruct);
  262.         }
  263.     }
  264.  
  265.     m_intList.Serialize(ar);
  266.  
  267.     m_dwArray.Serialize(ar);
  268.  
  269.     m_myobArray.Serialize(ar);
  270.  
  271.     m_ptArray.Serialize(ar);
  272.  
  273.     m_mapStringToString.Serialize(ar);
  274.  
  275.     m_mapStringToMyObject.Serialize(ar);
  276.  
  277.     m_mapDWordToMyStruct.Serialize(ar);
  278. }
  279.  
  280. /////////////////////////////////////////////////////////////////////////////
  281. // CCollectDoc diagnostics
  282.  
  283. #ifdef _DEBUG
  284. void CCollectDoc::AssertValid() const
  285. {
  286.     CDocument::AssertValid();
  287. }
  288.  
  289. void CCollectDoc::Dump(CDumpContext& dc) const
  290. {
  291.     CDocument::Dump(dc);
  292. }
  293. #endif //_DEBUG
  294.  
  295. /////////////////////////////////////////////////////////////////////////////
  296. // CCollectDoc commands
  297.  
  298. /////////////////////////////////////////////////////////////////////////////
  299. // Common DDX function
  300.  
  301. void AFXAPI DDX_Char(CDataExchange* pDX, int nIDC, TCHAR& ch)
  302. {
  303.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  304.     if (pDX->m_bSaveAndValidate)
  305.     {
  306.         TCHAR szText[10];
  307.         ::GetWindowText(hWndCtrl, szText, 10);
  308.         ch = szText[0];
  309.     }
  310.     else
  311.     {
  312.         TCHAR szText[10];
  313.         szText[0] = ch;
  314.         szText[1] = 0;
  315.         ::SetWindowText(hWndCtrl, szText);
  316.     }
  317. }
  318.